package product;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

public class Show {
    private int id;
    private Movie movie;
    private String hall;
    private Date time;
    private double price;
    private int rowCount;
    private int colCount;
    private Set<Seat> soldSeats; // 已售出的座位

    public Show(int id, Movie movie, String hall, Date time, double price, int rowCount, int colCount) {
        this.id = id;
        this.movie = movie;
        this.hall = hall;
        this.time = time;
        this.price = price;
        this.rowCount = rowCount;
        this.colCount = colCount;
        this.soldSeats = new HashSet<>();
    }

    // 检查座位是否可用
    public boolean checkSeatAvailability(int row, int col) {
        if (row < 1 || row > rowCount || col < 1 || col > colCount) {
            return false; // 座位不存在
        }
        return !soldSeats.contains(new Seat(row, col));
    }

    // 占用座位
    public void occupySeats(Set<Seat> seats) {
        for (Seat seat : seats) {
            if (checkSeatAvailability(seat.getRow(), seat.getCol())) {
                soldSeats.add(seat);
            }
        }
    }

    // 释放座位
    public void releaseSeat(Seat seat) {
        soldSeats.remove(seat);
    }

    // 显示座位状态
    public void displaySeats() {
        for (int i = 1; i <= rowCount; i++) {
            for (int j = 1; j <= colCount; j++) {
                if (soldSeats.contains(new Seat(i, j))) {
                    System.out.print("X "); // 已售出
                } else {
                    System.out.print("O "); // 可售
                }
            }
            System.out.println();
        }
    }

    // 获取可用座位数
    public int getAvailableSeatsCount() {
        return rowCount * colCount - soldSeats.size();
    }

    // 检查是否有售出的票
    public boolean hasSoldTickets() {
        return !soldSeats.isEmpty();
    }

    // 将座位信息转换为字符串，用于保存到文件
    public String seatsToString() {
        StringBuilder sb = new StringBuilder();
        for (Seat seat : soldSeats) {
            sb.append(seat.getRow()).append(",").append(seat.getCol()).append(";");
        }
        return sb.toString();
    }

    // 从字符串恢复座位信息
    public void stringToSeats(String seatsStr) {
        if (seatsStr == null || seatsStr.isEmpty()) {
            return;
        }

        soldSeats.clear();
        String[] seats = seatsStr.split(";");
        for (String seat : seats) {
            if (seat.isEmpty()) continue;
            String[] coords = seat.split(",");
            if (coords.length == 2) {
                try {
                    int row = Integer.parseInt(coords[0]);
                    int col = Integer.parseInt(coords[1]);
                    soldSeats.add(new Seat(row, col));
                } catch (NumberFormatException e) {
                    // 忽略格式错误的座位信息
                }
            }
        }
    }

    // Getter和Setter
    public int getId() { return id; }
    public void setId(int id) { this.id = id; }
    public Movie getMovie() { return movie; }
    public void setMovie(Movie movie) { this.movie = movie; }
    public String getHall() { return hall; }
    public void setHall(String hall) { this.hall = hall; }
    public Date getTime() { return time; }
    public void setTime(Date time) { this.time = time; }
    public double getPrice() { return price; }
    public void setPrice(double price) { this.price = price; }
    public int getRowCount() { return rowCount; }
    public int getColCount() { return colCount; }
    public Set<Seat> getSoldSeats() { return new HashSet<>(soldSeats); }
}